package com.thon.service.project.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectCategoryDao;
import com.thon.dao.project.ProjectDao;
import com.thon.entity.project.ProjectCategory;
import com.thon.service.project.ProjectCategoryService;

/**
 * @file ProjectCategoryServiceImpl.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月20日 上午10:16:35
 * @description: TODO
 */
@Service
@Transactional
public class ProjectCategoryServiceImpl implements ProjectCategoryService {

	@Autowired
	private ProjectCategoryDao projectCategoryDao;
	@Autowired
	private ProjectDao projectDao;

	@Override
	public List<ProjectCategory> listProjectCategories() {
		List<ProjectCategory> ProjectCategoryList= projectCategoryDao.listCategories();	
	
		
		if(ProjectCategoryList!=null)
		for(ProjectCategory projectCategory:ProjectCategoryList)
		{
			projectCategory.setProjectAmount(projectDao.countProjectByCategory(projectCategory.getId(),5));
		}
				
		return ProjectCategoryList;
	}

	@Override
	public ProjectCategory getProjectCategory(int id) {
		return projectCategoryDao.get(id);
	}

	@Override
	public void addProjectCategory(ProjectCategory projectCategory) {
		projectCategoryDao.save(projectCategory);
	}

	@Override
	public void updateProjectCategory(ProjectCategory projectCategory) {
		
		projectCategoryDao.update(projectCategory);
		
	}

	@Override
	public List<ProjectCategory> listCategoriesByType(String type) {
		
		return projectCategoryDao.listCategoriesByType(type);
	}

	@Override
	public void addCategory(ProjectCategory projectCategory) {
		projectCategoryDao.save(projectCategory);
		
	}

	@Override
	public void deleteCategory(int categoryId) {
		
		projectCategoryDao.delete(categoryId);
	}
	

	@Override
	public void updateAmount(Map<Integer, Integer> projects) {

		for(Integer projectCategoryId:projects.keySet()){
			ProjectCategory projectCategory = projectCategoryDao.get(projectCategoryId);
			projectCategory.setProjectAmount((long)(projects.get(projectCategoryId)));
		    projectCategoryDao.update(projectCategory);
		}
		
	}
	
	public Map<Integer, Integer> getAmount(){
		
		List<ProjectCategory> projectCategoryList = listAllProjectCategories();
		Map<Integer,Integer> categoryAmount = new HashMap<Integer,Integer>();
		for(ProjectCategory projectCategory:projectCategoryList)
		{
			categoryAmount.put(projectCategory.getId(),0);
		}
		return categoryAmount;
	}

	public List<ProjectCategory> listAllProjectCategories(){
		
		return projectCategoryDao.listAllCategories();		
		
	}
}
